#include "processpool.hpp"
#include <memory>

int main()
{
    ProcessPool pp(5);
    pp.InitProcessPool([](int fd)
                       {
        while(true)
        {
            int code = 0;
            // cout << "process blocking : " << getpid() << endl;
            ssize_t n = read(fd, &code, sizeof(code));
            if(n == sizeof(code))
            {
                cout << "Subprocess is awakened : " << getpid() << endl;
                if(code >= 0 && code < (int)tasks.size())
                {
                    tasks[code]();
                }
                else
                {
                    cerr << "invalid task code : " << code << endl;
                }
            }
            else if (n == 0)
            {
                cout << "pipe closed, exiting Subprocess : " << getpid() << endl;
                break;
            }
            else
            {
                perror("read error");
                exit(1);
            }
        } });
    pp.PollingCtrSubProcess(10);
    cout << "parent process : " << getpid() << " exit " << endl;
    pp.WaitSubProcess();
    // for (auto &ch : channels)
    // {
    //     ch.DebugPrint();
    // }
    return 0;
}